const http = require('http');

const BASE_URL = 'http://localhost:5000/api';

// 测试智能解析接口
async function testSmartParse() {
  console.log('🧠 测试智能解析接口...');
  
  const postData = JSON.stringify({
    text: '明天下午3点开会讨论项目进度，需要准备PPT'
  });

  const options = {
    hostname: 'localhost',
    port: 5000,
    path: '/api/ai/smart-parse',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(postData)
    }
  };

  return new Promise((resolve, reject) => {
    const req = http.request(options, (res) => {
      let data = '';
      res.on('data', (chunk) => {
        data += chunk;
      });
      res.on('end', () => {
        try {
          const result = JSON.parse(data);
          console.log('✅ 智能解析结果:', JSON.stringify(result, null, 2));
          resolve(result);
        } catch (error) {
          console.error('❌ 解析响应失败:', error);
          reject(error);
        }
      });
    });

    req.on('error', (error) => {
      console.error('❌ 请求失败:', error);
      reject(error);
    });

    req.write(postData);
    req.end();
  });
}

// 测试任务分解接口
async function testDecompose() {
  console.log('\n🔧 测试任务分解接口...');
  
  const postData = JSON.stringify({
    title: '完成项目报告',
    description: '需要编写一个完整的项目进度报告，包括技术实现、时间安排和风险评估'
  });

  const options = {
    hostname: 'localhost',
    port: 5000,
    path: '/api/ai/decompose',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(postData)
    }
  };

  return new Promise((resolve, reject) => {
    const req = http.request(options, (res) => {
      let data = '';
      res.on('data', (chunk) => {
        data += chunk;
      });
      res.on('end', () => {
        try {
          const result = JSON.parse(data);
          console.log('✅ 任务分解结果:', JSON.stringify(result, null, 2));
          resolve(result);
        } catch (error) {
          console.error('❌ 解析响应失败:', error);
          reject(error);
        }
      });
    });

    req.on('error', (error) => {
      console.error('❌ 请求失败:', error);
      reject(error);
    });

    req.write(postData);
    req.end();
  });
}

// 测试智能建议接口
async function testSuggestions() {
  console.log('\n💡 测试智能建议接口...');
  
  const options = {
    hostname: 'localhost',
    port: 5000,
    path: '/api/ai/suggestions',
    method: 'GET'
  };

  return new Promise((resolve, reject) => {
    const req = http.request(options, (res) => {
      let data = '';
      res.on('data', (chunk) => {
        data += chunk;
      });
      res.on('end', () => {
        try {
          const result = JSON.parse(data);
          console.log('✅ 智能建议结果:', JSON.stringify(result, null, 2));
          resolve(result);
        } catch (error) {
          console.error('❌ 解析响应失败:', error);
          reject(error);
        }
      });
    });

    req.on('error', (error) => {
      console.error('❌ 请求失败:', error);
      reject(error);
    });

    req.end();
  });
}

// 测试智能搜索接口
async function testSearch() {
  console.log('\n🔍 测试智能搜索接口...');
  
  const postData = JSON.stringify({
    query: '高优先级任务'
  });

  const options = {
    hostname: 'localhost',
    port: 5000,
    path: '/api/ai/search',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(postData)
    }
  };

  return new Promise((resolve, reject) => {
    const req = http.request(options, (res) => {
      let data = '';
      res.on('data', (chunk) => {
        data += chunk;
      });
      res.on('end', () => {
        try {
          const result = JSON.parse(data);
          console.log('✅ 智能搜索结果:', JSON.stringify(result, null, 2));
          resolve(result);
        } catch (error) {
          console.error('❌ 解析响应失败:', error);
          reject(error);
        }
      });
    });

    req.on('error', (error) => {
      console.error('❌ 请求失败:', error);
      reject(error);
    });

    req.write(postData);
    req.end();
  });
}

// 测试个性化建议接口
async function testPersonalizedSuggestions() {
  console.log('\n👤 测试个性化建议接口...');
  
  const postData = JSON.stringify({
    userId: 'user123',
    context: '工作日上午，需要提高效率'
  });

  const options = {
    hostname: 'localhost',
    port: 5000,
    path: '/api/ai/personalized-suggestions',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(postData)
    }
  };

  return new Promise((resolve, reject) => {
    const req = http.request(options, (res) => {
      let data = '';
      res.on('data', (chunk) => {
        data += chunk;
      });
      res.on('end', () => {
        try {
          const result = JSON.parse(data);
          console.log('✅ 个性化建议结果:', JSON.stringify(result, null, 2));
          resolve(result);
        } catch (error) {
          console.error('❌ 解析响应失败:', error);
          reject(error);
        }
      });
    });

    req.on('error', (error) => {
      console.error('❌ 请求失败:', error);
      reject(error);
    });

    req.write(postData);
    req.end();
  });
}

// 主测试函数
async function runAllTests() {
  try {
    console.log('🚀 开始测试AI相关接口...\n');
    
    await testSmartParse();
    await testDecompose();
    await testSuggestions();
    await testSearch();
    await testPersonalizedSuggestions();
    
    console.log('\n🎉 所有AI接口测试完成！');
  } catch (error) {
    console.error('\n❌ 测试过程中出现错误:', error);
  }
}

// 运行测试
runAllTests();
